package com.mjf.io.output;

import org.junit.Test;

import java.io.*;

/**
 * 文件拷贝
 */
public class FileCopy {

    /**
     * 字节流拷贝文件
     */
    @Test
    public void fileCopyWithByteStream() {
        String inputImage = "D:\\coding\\idea\\java-stu\\input\\image-rendered.png";
        String outputImage = "D:\\coding\\idea\\java-stu\\output\\image-rendered.png";

        int readLen = 0;    // 读取到的文件大小
        byte[] buf = new byte[1024];    // 缓冲区

        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(inputImage);
            fos = new FileOutputStream(outputImage);
            while ((readLen = fis.read(buf)) != -1) {
//                fos.write(buf);   // 最后一次读取到的字节不够 1024 时，有可能导致文件损坏
                fos.write(buf, 0, readLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字节处理流拷贝文件
     */
    @Test
    public void fileCopyWithByteBufferedStream() {
        String inputImage = "D:\\coding\\idea\\java-stu\\src\\main\\java\\com\\mjf\\io_input\\FileReaderUse.java";
        String outputImage = "D:\\coding\\idea\\java-stu\\output\\hello4.txt";

        int readLen;
        byte[] buf = new byte[1024];

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(inputImage));
            bos = new BufferedOutputStream(new FileOutputStream(outputImage));
            while ((readLen = bis.read(buf)) != -1) {
                bos.write(buf, 0, readLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字符流拷贝文件
     */
    @Test
    public void fileCopyWithCharStream() {
        String inputImage = "D:\\coding\\idea\\java-stu\\src\\main\\java\\com\\mjf\\io_input\\FileReaderUse.java";
        String outputImage = "D:\\coding\\idea\\java-stu\\output\\hello4.txt";

        int readLen;
        char[] buf = new char[1024];

        FileReader fr = null;
        FileWriter fw = null;

        try {
            fr = new FileReader(inputImage);
            fw = new FileWriter(outputImage);
            while ((readLen = fr.read(buf)) != -1) {
                fw.write(buf, 0, readLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
                if (fw != null) {
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字符处理流拷贝文件:字符流处理字节文件导致文件损坏
     */
    @Test
    public void fileCopyWithCharBufferedStream() {
        String inputImage = "D:\\coding\\idea\\java-stu\\input\\image-rendered.png";
        String outputImage = "D:\\coding\\idea\\java-stu\\output\\image-rendered2.png";

        String line;

        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            br = new BufferedReader(new FileReader(inputImage));
            bw = new BufferedWriter(new FileWriter(outputImage));
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}


